
package com.ami.vmedia;

import com.ami.iusb.IUSBRedirSession;
import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.JViewer;
import com.ami.kvm.jviewer.gui.JViewerApp;
import com.ami.kvm.jviewer.gui.LocaleStrings;
import com.ami.kvm.jviewer.gui.customizefilechooser;
import com.ami.vmedia.gui.DeviceControlPanel;
import com.ami.vmedia.gui.DevicePanel;
import com.ami.vmedia.gui.StatusTabPanel;
import com.ami.vmedia.gui.VMPane;
import java.awt.Component;
import java.io.File;
import java.io.PrintStream;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class RedirectionController {
	private String sessionToken = null;

	private int cdPort;

	private int fdPort;
	private int hdPort;
	private boolean useSSL;

	public RedirectionController() {
		this.cdPort = JViewer.getCDPort();
		this.fdPort = JViewer.getFDPort();
		this.hdPort = JViewer.getHDPort();
		this.useSSL = JViewer.isVMUseSSL();
	}

	public void handleDeviceRedirection(int paramInt1, int paramInt2) {
		IUSBRedirSession localIUSBRedirSession = VMApp.getInstance().getIUSBRedirSession();
		this.sessionToken = JViewer.getKVMToken();
		boolean bool1 = false;
		boolean bool2 = false;
		DevicePanel localDevicePanel = VMApp.getVMPane().getDeviceControlPanel(paramInt1, paramInt2).getDevicePanel();
		String str1 = null;
		int i = 0;
		switch (paramInt1) {
		case 0:
			i = this.cdPort;
			break;
		case 1:
			i = this.fdPort;
			break;
		case 2:
			i = this.hdPort;
		}

		if (localDevicePanel.isImageSelected()) {
			Debug.out.println("Starting image redirection " + localDevicePanel.getImagePath());
			bool2 = false;
			str1 = localDevicePanel.getImagePath();
		} else if (localDevicePanel.isFolderMountSelected()) {
			bool2 = false;

			double d = localDevicePanel.getDirSize(new File(localDevicePanel.folderTxt.getText()));
			int j = 1;
			for (int k = 0; k < 8; k++) {
				if (d / 1024.0D < j) {
					break;
				}

				j *= 2;
			}
			localDevicePanel.setFolderImageSize(Integer.toString(j));

			if (VMApp.getVMPane().getDeviceControlPanel(paramInt1, paramInt2).getConnectButton()
					.getText() == LocaleStrings.getString("AJ_4_DCP")) {
				File localFile = new File(localDevicePanel.getFolderImagePath());

				long l1 = localFile.getTotalSpace();

				long l2 = localFile.getUsableSpace();

				localDevicePanel.setFreeDiskSize(l2);

				if (Long.parseLong(localDevicePanel.getFolderImageSize()) * 1024L >= localDevicePanel.getFreeDiskSize()
						/ 1048576L) {
					System.out.println("imageSize=" + Long.parseLong(localDevicePanel.getFolderImageSize()) * 1024L
							+ "--------------freedisk=" + Long.toString(localDevicePanel.getFreeDiskSize() / 1048576L));
					String str2 = LocaleStrings.getString("AJ_8_DCP");
					String str3 = LocaleStrings.getString("AJ_7_DCP");
					str2 = str2 + localDevicePanel.getFolderImageSize();
					str2 = str2 + LocaleStrings.getString("AJ_9_DCP");

					int m = JOptionPane.showConfirmDialog(null, str2, str3, 0);
					if (m == 0) {
						String str4 = null;

						JFileChooser localJFileChooser = new JFileChooser(str4);
						localJFileChooser.setFileSelectionMode(1);
						customizefilechooser localcustomizefilechooser = new customizefilechooser();
						localcustomizefilechooser.customizeFileChooser(localJFileChooser);

						Object localObject = null;

						if (JViewer.isVMApp()) {
							VMApp.getInstance();
							localObject = VMApp.getVMFrame();
						} else {
							localObject = JViewerApp.getInstance().getVMDialog();
						}

						int n = localJFileChooser.showDialog((Component) localObject,
								LocaleStrings.getString("V_8_VRS"));
						if (n == 0) {
							if (localJFileChooser.getSelectedFile().exists()) {
								str4 = localJFileChooser.getSelectedFile().toString();
							} else
								str4 = localJFileChooser.getSelectedFile().getParent();
						}
						localDevicePanel.setFolderImagePath(str4);
						return;
					}

					return;
				}
			}

			str1 = localDevicePanel.getFolderImageSize() + " : " + localDevicePanel.folderTxt.getText() + " : "
					+ localDevicePanel.getFolderImagePath();
		} else {
			Debug.out.println("Starting device redirection " + localDevicePanel.getSelectedDeviceString());
			bool2 = true;
			str1 = localDevicePanel.getSelectedDeviceString();
		}
		if (localIUSBRedirSession.getDeviceRedirStatus(paramInt1, paramInt2) == 0) {

			localDevicePanel.disableAll();
			// XXX by tianlin for [CD执行] 参数：0，sessionToken, cdport, 0, false(vmsecure==1), CD Image path, false
			bool1 = localIUSBRedirSession.startDeviceRedirection(paramInt1, this.sessionToken, i, paramInt2,
					this.useSSL, str1, bool2);
			if (bool1) {
				if (!bool2) {
					String[] arrayOfString = localDevicePanel.getImagePathList();
					localDevicePanel.saveImagePath(arrayOfString, paramInt1, paramInt2);

				} else {
					VMApp.getVMPane().physicalDriveChangeState(paramInt1, paramInt2, str1, false);
				}

				localIUSBRedirSession.setImageRedirected(paramInt1, paramInt2, !bool2);
				VMApp.getVMPane().getStatusTabPanel().updateStatusTable(paramInt1, paramInt2, true);
				VMApp.getVMPane().updateDeviceStatusTable(paramInt1, paramInt2, true);
			}

			localDevicePanel.enableAll();
			Debug.out.println("redirection should be running");
		} else {
			localIUSBRedirSession.setImageRedirected(paramInt1, paramInt2, !bool2);
			localIUSBRedirSession.setDeviceStopMode(paramInt1, paramInt2, true);
			if (bool2) {
				VMApp.getVMPane().physicalDriveChangeState(paramInt1, paramInt2, str1, true);
			}

			localIUSBRedirSession.stopDeviceRedirection(paramInt1, paramInt2);
			VMApp.getVMPane().getStatusTabPanel().updateStatusTable(paramInt1, paramInt2, false);
			VMApp.getVMPane().updateDeviceStatusTable(paramInt1, paramInt2, false);
		}
		VMApp.getVMPane().getDeviceControlPanel(paramInt1, paramInt2).updateDeviceControlPanel();
	}
}

/*
 * Location:
 * D:\Users\tianlin\Downloads\JViewer.jar!\com\ami\vmedia\RedirectionController.
 * class Java compiler version: 5 (49.0) JD-Core Version: 0.7.1
 */